package cn.dankal.flyaudio.friend.utils;

import com.blankj.utilcode.util.StringUtils;

import java.util.List;

import cn.dankal.basiclib.util.JMessageToastUtils;
import cn.dankal.flyaudio.friend.pickimage.StringUtil;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.callback.RequestCallback;
import cn.jpush.im.android.api.model.GroupMemberInfo;

public class GroupManagerUtils {

    /**
     * 通过用户名判断用户是否再群内
     *
     * @param username 需要判断的用户名
     * @param groupId  群组id
     * @param listener 回调
     */
    public static void inGroupMembers(String username, int groupId, CommonBooleanResultListener listener) {
        JMessageClient.getGroupMembers(groupId, new RequestCallback<List<GroupMemberInfo>>() {
            @Override
            public void gotResult(int i, String s, List<GroupMemberInfo> groupMemberInfos) {
                if (i == 0) {
                    //获取群成员成功
                    for (GroupMemberInfo groupMemberInfo : groupMemberInfos) {
                        if (StringUtils.equals(groupMemberInfo.getUserInfo().getUserName(), username)) {
                            if (listener != null) {
                                listener.onResult(true);
                            }
                            break;
                        }
                    }
                    if (listener != null) {
                        listener.onResult(false);
                    }
                } else {
                    JMessageToastUtils.errorToast(i);
                    if (listener != null) {
                        listener.onResult(false);
                    }
                }
            }
        });
    }


    /**
     * 通用异步返回boolean结果接口
     */
    public interface CommonBooleanResultListener {

        void onResult(boolean isSuccess);

    }
}
